package com.sxz.sbs.module.message;


import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sxz.sbs.R;
import com.sxz.sbs.app.AppConstant;
import com.sxz.sbs.applibrary.state.StateLayout;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.event.JPushRouseEvent;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.message.adapter.MyMessageAdapter;
import com.sxz.sbs.module.message.mode.MyMessageBean;
import com.sxz.sbs.module.message.presenter.MyMessagePresenter;
import com.sxz.sbs.module.message.view.MyMessageView;
import com.sxz.sbs.router.RouterUrl;
import com.sxz.sbs.widget.CommonTitle;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * 我的消息
 */
@Route(path = RouterUrl.MY_NEWS)
public class MyMessageActivity extends BaseActivity<MyMessagePresenter> implements MyMessageView,SwipeRefreshLayout.OnRefreshListener{

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.stateLayout)
    StateLayout stateLayout;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    private MyMessageAdapter adapter;
    private List<MyMessageBean> dataList = new ArrayList<>();
    public static boolean isForeground = false;
    @Override
    protected void init() {
        new CommonTitle.Builder(this).setTitle("消息列表").showMore(false).build().show();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        refreshLayout.setColorSchemeResources(R.color.color_main);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setRefreshing(true);
        adapter = new MyMessageAdapter(dataList);
        recyclerView.setAdapter(adapter);
        recyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                EventBus.getDefault().post(new JPushRouseEvent());
                ARouter.getInstance().build(RouterUrl.MESSAGE_LIST)
                        .withInt("msgType",dataList.get(position).getValue())
                        .navigation();
            }
        });
        stateLayout.setOnRetryListener(new StateLayout.OnRetryListener() {
            @Override
            public void onRetry(int state) {
                refreshLayout.setRefreshing(true);
                getData();
            }
        });
    }

    @Override
    protected void onResume() {
        isForeground = true;
        super.onResume();
        getData();
    }

    /**
     * 获取数据
     */
    public void getData() {
        HashMap<String,Object> map = HttpParamMap.getParamMap();
        mPresenter.getMessageType(map);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_message;
    }

    @Override
    protected MyMessagePresenter createPresenter() {
        return new MyMessagePresenter();
    }
    @Override
    public void loginAgain() {
        super.loginAgain();
        refreshLayout.setRefreshing(false);
    }
    @Override
    public void showError(int errorTag, String msg) {
        refreshLayout.setRefreshing(false);
        if (errorTag == AppConstant.NET_ERROR_CODE) {
            stateLayout.showNetwork();
        } else {
            stateLayout.showEmpty();
        }
    }


    @Override
    protected void onDestroy() {
        isForeground = false;
        super.onDestroy();
    }

    @Override
    public void onRefresh() {
        getData();
    }

    @Override
    public void onGetMyMessageSuccess(List<MyMessageBean> data) {
        if (mIsLive ){
            refreshLayout.setRefreshing(false);
            if (data != null) {
                stateLayout.showContent();
                dataList.clear();
                dataList.addAll(data);
                adapter.notifyDataSetChanged();
            }
        }
    }
}
